const URL_HEAD = "http://localhost:8080/";
function onUserManageLoad() {
    let tableBuilder = new UserTableBuilder();
	$.ajax({
	    url:URL_HEAD + "user/findAll",
	    //通过type来判断调用哪个方法
	    type: "post",
	    dataType: "json",
	    success: function (result){
			for(let i=0; i < result.data.length;i++){
				tableBuilder.addUserRow(i+1,result.data[i].id,result.data[i].
										username,result.data[i].email," ",
										result.data[i].type,result.data[i].password);
			}
			tableBuilder.build();
		
	    },
	    error: function (xhr, textStatus, errorThrown){
	        showError("用户注册异常", xhr, textStatus, errorThrown);
	    }
	});
    // tableBuilder.addUserRow(1, "1", "张三", "zhangsan@sina.cn", 12, 0, "123456");
    // tableBuilder.addUserRow(2, "2", "李四", "lisi@163.com", 21, 1, "654321");
    // tableBuilder.build();
}
function onUserCenterLoad(){
    const uuid = getUrlParameter("uuid");
    $.ajax({
        url:URL_HEAD + "token/findUserAllByUuid",
        //通过type来判断调用哪个方法
        type: "post",
        data: {"uuid": uuid},
        dataType: "json",
        success: function (result){
			document.getElementById("u63_input").value = result.data.username;
			document.getElementById("u52_input").value = result.data.email;
			if(result.data.type==0){
				document.getElementById("u61_input").value = "管理者";
				window.location.href = URL_HEAD + 'admincenter.html?uuid=' + uuid;
			}else if(result.data.type==1){
				document.getElementById("u61_input").value = "开发者";
            }else{
				document.getElementById("u61_input").value = "普通用户";
			}
        },
        error: function (xhr, textStatus, errorThrown){
            //showError("异常", xhr, textStatus, errorThrown);
        }
    });
}

function onHomePageLoad(){
    const uuid = getUrlParameter("uuid");
    const type = selectTypeByUuid(uuid);
}
//注册
function  onLogon(){
    const userName = $("#u21_input").val();
    const passWord = $("#u22_input").val();
    const email = $("#u27_input").val();
    const type = $("#u36_input").val();
    $.ajax({
        url:URL_HEAD + "user/logon",
        //通过type来判断调用哪个方法
        type: "post",
        data: {"username": userName , "password": passWord, "email": email,"type":type},
        dataType: "json",
        success: function (result){
            alert("注册成功");
            window.location.href = URL_HEAD + 'login.html';
        },
        error: function (xhr, textStatus, errorThrown){
            showError("用户注册异常", xhr, textStatus, errorThrown);
        }
    });
}
//登录
function  onLogin(){
    const userName = $("#u10_input").val();
    const passWord = $("#u11_input").val();
    $.ajax({
        url:URL_HEAD + "user/login",
        //通过type来判断调用哪个方法
        type: "post",
        data: {"username": userName , "password": passWord},
        dataType: "json",
        success: function (result){
            if(result.status==0){
                window.location.href = URL_HEAD + 'index.html?uuid=' + result.data;
            }
        },
        error: function (xhr, textStatus, errorThrown){
            showError("用户登录异常", xhr, textStatus, errorThrown);
        }
    });
}
//登录页面跳转找回密码页面
function goFindPassWord(){
    window.location.href = URL_HEAD + 'findpassword.html';
}
//找回密码
function onFindPassWord(){
    const email = $("#u38_input").val();
    $.ajax({
        url:URL_HEAD + "user/findByEmail",
        //通过type来判断调用哪个方法
        type: "post",
        data: {"email": email},
        dataType: "json",
        success: function (result){
            if(result.status==0){
                alert("用户名：" + result.data.username + "密码：" + result.data.password);
            }
        },
        error: function (xhr, textStatus, errorThrown){
            showError("用户登录异常", xhr, textStatus, errorThrown);
        }
    });
}
function updateEmailByUuid(){
	const uuid = getUrlParameter("uuid");
	const email = document.getElementById("u52_input").value;
	$.ajax({
	    url:URL_HEAD + "token/updateEmailByUuid",
	    //通过type来判断调用哪个方法
	    type: "post",
	    data: {"uuid": uuid, "email": email},
	    dataType: "json",
	    success: function (result){
			onUserCenterLoad();
	    },
	    error: function (xhr, textStatus, errorThrown){
	        //showError("异常", xhr, textStatus, errorThrown);
	    }
	});
}
function updateTypeByUuid(){
	const uuid = getUrlParameter("uuid");
	let typeValue = document.getElementById("u61_input").value;
	if(typeValue == "管理员"){
		var type = 0;
	}else if(typeValue == "开发者"){
		var type = 1;
	}else{
		var type = 2;
	}
	$.ajax({
	    url:URL_HEAD + "token/updateTypeByUuid",
	    //通过type来判断调用哪个方法
	    type: "post",
	    data: {"uuid": uuid, "type": type},
	    dataType: "json",
	    success: function (result){
			onUserCenterLoad();
	    },
	    error: function (xhr, textStatus, errorThrown){
	        //showError("异常", xhr, textStatus, errorThrown);
	    }
	});
}

function goLogin(){
	window.location.href = URL_HEAD;
}
function goCenter() {
    let uuid = getUrlParameter("uuid");
    $.ajax({
        url:URL_HEAD + "token/selectTypeByUuid",
        //通过type来判断调用哪个方法
        type: "post",
        data: {"uuid": uuid},
        dataType: "json",
        success: function (result){
            let type = result.data;
			if(type==0){
                window.location.href = URL_HEAD + 'admincenter.html?uuid=' + uuid;
			}else if(type==null){
                window.location.href = URL_HEAD + 'login.html';
            }else{
                window.location.href = URL_HEAD + 'usercenter.html?uuid=' + uuid;
			}
        },
        error: function (xhr, textStatus, errorThrown){
            //showError("异常", xhr, textStatus, errorThrown);
        }
    });
}
function goLogin(){
    window.location.href = URL_HEAD + 'login.html';
}
function goManage() {
    let uuid = getUrlParameter("uuid");
    $.ajax({
        url:URL_HEAD + "token/selectTypeByUuid",
        //通过type来判断调用哪个方法
        type: "post",
        data: {"uuid": uuid},
        dataType: "json",
        success: function (result){
            let type = result.data;
            if(type==1){
                window.location.href = URL_HEAD + 'developdevicemanage.html?uuid=' + uuid;
            }else if(type==null){
                window.location.href = URL_HEAD + 'login.html';
            }else{
                window.location.href = URL_HEAD + 'userdevicemanage.html?uuid=' + uuid;
			}
        },
        error: function (xhr, textStatus, errorThrown){
            showError("异常", xhr, textStatus, errorThrown);
        }
    });
}


function getUrlParameter(name){
    name = name.replace(/[]/,"\[").replace(/[]/,"\[").replace(/[]/,"\\\]");
    const regexS = "[\\?&]"+name+"=([^&#]*)";
    const regex = new RegExp( regexS );
    const results = regex.exec(window.parent.location.href );
    if( results == null ) return ""; else {
        return results[1];
    }
}

function onModifyUserClick(index) {
	const id = index + 1;
	const userName = $("#txtUserName-" + index).val();	
	const passWord = $("#txtPassword-" + index).val();
	//const email = $("txtEmail-" + index).val();
	const email = document.getElementById("txtEmail-" + index).value;
	const type = $("#selRole-" + index).val();
	$.ajax({
	    url:URL_HEAD + "user/modifyUser",
	    //通过type来判断调用哪个方法
	    type: "post",
		data: {"id": id , "username": userName , "password": passWord, "email": email , "type": type},
	    dataType: "json",
	    success: function (result){
			location.reload();
			document.getElementById("u90_input").value = "";
			document.getElementById("u91_input").value = "";
			document.getElementById("u92_input").value = "";
			document.getElementById("u94_input").value = "";
	    },
	    error: function(xhr, textStatus, errorThrown){
	        showError("用户修改异常", xhr, textStatus, errorThrown);
	    }
	});
}

function onDeleteUserClick(index) {
	const id = index;
	$.ajax({
	    url:URL_HEAD + "user/deleteUser",
	    //通过type来判断调用哪个方法
	    type: "post",
		data: {"id": id},
	    dataType: "json",
	    success: function (result){
			location.reload();
			document.getElementById("u90_input").value = "";
			document.getElementById("u91_input").value = "";
			document.getElementById("u92_input").value = "";
			document.getElementById("u94_input").value = "";
	    },
	    error: function(xhr, textStatus, errorThrown){
	        showError("用户删除异常", xhr, textStatus, errorThrown);
	    }
	});
}

function onAddUser(){
	const userName = document.getElementById("u90_input").value;	
	const passWord = document.getElementById("u94_input").value;
	const email = document.getElementById("u91_input").value;
	let type = document.getElementById("u93_input").value;
	if(type == "管理者")
		type = 0;
	if(type == "开发者")
		type = 1;
	if(type == "普通用户")
		type = 2;
	$.ajax({
	    url:URL_HEAD + "user/logon",
	    //通过type来判断调用哪个方法
	    type: "post",
        data: {"username": userName , "password": passWord, "email": email,"type":type},
	    dataType: "json",
	    success: function (result){
			location.reload();
			document.getElementById("u90_input").value = "";
			document.getElementById("u91_input").value = "";
			document.getElementById("u92_input").value = "";
			document.getElementById("u94_input").value = "";
	    },
	    error: function(xhr, textStatus, errorThrown){
	        showError("用户删除异常", xhr, textStatus, errorThrown);
	    }
	});
}

function UserTableBuilder() {
    this.builder = new StringBuilder();
    this.interval = 25;

    $("#u73-data").remove();
    this.builder.append('<div id="u73-data">');
}

UserTableBuilder.prototype.addUserRow = function (index, id, name, email, age, role, password) {
    //alert("index: " + index);
    var rUserIdId = "rUserId-" + index;
    var txtNameId = "txtUserName-" + index;
    var txtEmailId = "txtEmail-" + index;
    var txtAgeId = "txtAge-" + index;
    var selRoleId = "selRole-" + index;
    var txtPasswordId = "txtPassword-" + index;
    var btnModifyUserId = "btnModifyUser-" + index;
    var btnDeleteUserId = "btnDeleteUser-" + index;
    var rowTop = this.interval * (index - 1);

    //新的用户信息行
    this.builder.append('<div class="preeval" style="width: 646px; height: 25px;">');
    //用户ID
    this.builder.append('<div class="ax_default box_1 u81" data-label="rId" ');
    this.builder.append('style="width: 44px; height: 25px; left: 0px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u81_div" style="width: 44px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<div class="text u81_text" style="visibility: inherit">');
    this.builder.append('<p id="');
    this.builder.append(rUserIdId);
    this.builder.append('"><span style="text-decoration:none;">');
    this.builder.append(id);
    this.builder.append('</span></p>');
    this.builder.append('</div>');
    this.builder.append('</div>');
    //用户名
    this.builder.append('<div class="ax_default text_field u74" data-label="txtName" ');
    this.builder.append('style="width: 100px; height: 25px; left: 44px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u74_div" style="width: 100px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<input id="');
    this.builder.append(txtNameId);
    this.builder.append('" type="text" value="');
    this.builder.append(name);
    this.builder.append('" class="u74_input"/>');
    this.builder.append('</div>');
    //邮箱
    this.builder.append('<div class="ax_default text_field u75" data-label="txtEmail" ');
    this.builder.append('style="width: 155px; height: 25px; left: 144px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u75_div" style="width: 155px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<input id="');
    this.builder.append(txtEmailId);
    this.builder.append('" type="text" value="');
    this.builder.append(email);
    this.builder.append('" class="u75_input"/>');
    this.builder.append('</div>');
    //年龄
    this.builder.append('<div class="ax_default text_field u76" data-label="txtAge" ');
    this.builder.append('style="width: 67px; height: 25px; left: 299px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u76_div" style="width: 67px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<input id="');
    this.builder.append(txtAgeId);
    this.builder.append('" type="text" value="');
    this.builder.append(age);
    this.builder.append('" class="u76_input"/>');
    this.builder.append('</div>');
    //角色
    this.builder.append('<div class="ax_default droplist u77" data-label="selRole" ');
    this.builder.append('style="width: 100px; height: 25px; left: 366px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u77_div" style="width: 100px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<select id="');
    this.builder.append(selRoleId);
    this.builder.append('" class="u77_input">');
    this.builder.append('<option class="u77_input_option" ');
    if (role === 0) {
        this.builder.append('selected ');
    }
    this.builder.append('value="0">管理员</option>');
    this.builder.append('<option class="u77_input_option" ');
    if (role === 1) {
        this.builder.append('selected ');
    }
    this.builder.append('value="1">开发者</option>');
    this.builder.append('<option class="u77_input_option" ');
    if (role === 2) {
        this.builder.append('selected ');
    }
    this.builder.append('value="2">普通用户</option>');
    this.builder.append('</select>');
    this.builder.append('</div>');
    //密码
    this.builder.append('<div class="ax_default text_field u78" data-label="txtPassword" ');
    this.builder.append('style="width: 100px; height: 25px; left: 466px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit">');
    this.builder.append('<div class="u78_div" style="width: 100px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<input id="');
    this.builder.append(txtPasswordId);
    this.builder.append('" type="text" value="');
    this.builder.append(password);
    this.builder.append('" class="u78_input"/>');
    this.builder.append('</div>');
    //修改按钮
    this.builder.append('<div id="');
    this.builder.append(btnModifyUserId);
    this.builder.append('" class="ax_default button u79" data-label="btnModifyUser" ');
    this.builder.append('style="width: 40px; height: 25px; left: 566px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit" onclick="onModifyUserClick(');
    this.builder.append(index);
    this.builder.append(')">');
    this.builder.append('<div class="u79_div" style="width: 40px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<div class="text u79_text" style="visibility: inherit">');
    this.builder.append('<p><span style="text-decoration:none;">修改</span></p>');
    this.builder.append('</div>');
    this.builder.append('</div>');
    //删除按钮
    this.builder.append('<div id="');
    this.builder.append(btnDeleteUserId);
    this.builder.append('" class="ax_default button u80" data-label="btnDeleteUser" ');
    this.builder.append('style="width: 40px; height: 25px; left: 606px; top: ');
    this.builder.append(rowTop);
    this.builder.append('px;visibility: inherit" onclick="onDeleteUserClick(');
    this.builder.append(index);
    this.builder.append(')">');
    this.builder.append('<div class="u80_div" style="width: 40px; height: 25px;visibility: inherit"></div>');
    this.builder.append('<div class="text u80_text" style="visibility: inherit">');
    this.builder.append('<p><span style="text-decoration:none;">删除</span></p>');
    this.builder.append('</div>');
    this.builder.append('</div>');

    this.builder.append('</div>');
}

UserTableBuilder.prototype.build = function () {
    this.builder.append('</div>');
    var value = this.builder.toString();
    //console.log(value);
    $("#u73").append(value);
}

